package hsb.ruoyi.admin.web.controller.system;

import hsb.ruoyi.common.annotation.Log;
import hsb.ruoyi.common.core.controller.BaseController;
import hsb.ruoyi.common.core.domain.AjaxResult;
import hsb.ruoyi.common.core.domain.entity.SysDictData;
import hsb.ruoyi.common.core.page.TableDataInfo;
import hsb.ruoyi.common.enums.BusinessType;
import hsb.ruoyi.common.utils.StringUtils;
import hsb.ruoyi.system.service.ISysDictDataService;
import hsb.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Produces(MediaType.APPLICATION_JSON)
@Path("/system/dict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    //@PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GET
    @Path("/list")
    //@Produces(MediaType.APPLICATION_FORM_URLENCODED)
    public TableDataInfo list(@BeanParam SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    //@Log(title = "字典数据", businessType = BusinessType.EXPORT)
    //@PreAuthorize("@ss.hasPermi('system:dict:export')")
//    @POST @Path("/export")
//    public void export(HttpServletResponse response, SysDictData dictData)
//    {
//        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
//        util.exportExcel(response, list, "字典数据");
//    }

    /**
     * 查询字典数据详细
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GET
    @Path(value = "/{dictCode}")
    public AjaxResult getInfo(@PathParam("dictCode") Long dictCode) {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GET
    @Path(value = "/type/{dictType}")
    public AjaxResult dictType(@PathParam("dictType") String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 新增字典类型
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @POST
    public AjaxResult add(@Valid  SysDictData dict) {
        dict.setCreateBy(getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PUT
    public AjaxResult edit(@Valid  SysDictData dict) {
        dict.setUpdateBy(getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DELETE @Path("/{dictCodes}")
    public AjaxResult remove(@PathParam("dictCodes") Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
